<?php

namespace app\api\controller;


class Wechatapp
{

    /**
     * 微信小程序获取openid  询诊端
     */
    public function getWechatOpenid()
    {


        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($_hosp)) {
            $return['err'] = 1;
            $return['msg'] = '参数错误[_hosp]';
            return json($return);
        }
        $code = trim(input("code"));//用户auth
        $Config = config('WEIXIN_APP');
        $Config = $Config[$_hosp];
        $Config = $Config['userClient'];


        $appid = $Config['APPID'];
        $secret = $Config['APPSECRET'];
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";
        $res = $this->httpRequest($url);
        $res = json_decode($res, true);
        if ($res['session_key'] && $res['openid']) {
            $return['err'] = 0;
            $return['data'] = $res;
            $return['msg'] = '成功';
            return json($return);
        } else {
            $return['err'] = 1;
            $return['data'] = $res;
            $return['msg'] = '失败';
            return json($return);
        }
    }

    /**
     * 微信小程序获取openid  医生端
     */
    public function getDoctorWechatOpenid()
    {
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($_hosp)) {
            $return['err'] = 1;
            $return['msg'] = '参数错误[_hosp]';
            return json($return);
        }

        $code = trim(input("code"));//用户auth

        $Config = config('WEIXIN_APP');
        $Config = $Config[$_hosp];
        $Config = $Config['doctorClient'];

        $appid = $Config['APPID'];
        $secret = $Config['APPSECRET'];
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";
        $res = $this->httpRequest($url);
        $res = json_decode($res, true);
        if ($res['session_key'] && $res['openid']) {
            $return['err'] = 0;
            $return['data'] = $res;
            $return['msg'] = '成功';
            return json($return);
        } else {
            $return['err'] = 1;
            $return['data'] = $res;
            $return['msg'] = '失败';
            return json($return);
        }
    }


    /**
     * 绑定用户接口  医生端
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bindDoctorUser()
    {
        $access_token = trim(input("request.access_token"));//小程序token
        $wechat_session_key = trim(input("request.session_key"));//小程序token
        $avatar_url = trim(input("request.avatar_url"));//头像
        $nickname = trim(input("request.nickname"));//
        $sex = trim(input("request.sex"));//
        $phone = trim(input("request.phone"));//
        $rcode = trim(input("request.rcode"));//
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($_hosp)) {
            $return['err'] = 1;
            $return['msg'] = '参数错误[_hosp]';
            return json($return);
        }

        if (empty($access_token)) {
            $return['err'] = 10000;
            $return['msg'] = 'access_token错误';
            return json($return);
        }

        if (empty($wechat_session_key)) {
            $return['err'] = 10000;
            $return['msg'] = 'session_key错误';
            return json($return);
        }
        if (empty($phone)) {
            $return['err'] = 1;
            $return['msg'] = '手机号不能为空';
            return json($return);
        }
        if (empty($rcode)) {
            $return['err'] = 1;
            $return['msg'] = '验证码不能为空';
            return json($return);
        }

        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }


        //根据手机号检查用户
        $dataModel = new \app\admin\model\User();

        $phoneUserInfo = $dataModel->getDataForWhere(["uname" => $phone, 'is_delete' => 0]);

        if (empty($phoneUserInfo)) {
            $return['err'] = 1;
            $return['msg'] = '系统检查到该手机号用户不存在';
            return json($return);
        }
        if (empty($phoneUserInfo['is_doctor'])) {
            $return['err'] = 1;
            $return['msg'] = '系统检查到该手机号用户不是医生';
            return json($return);
        }
        $doctorModel = new \app\admin\model\HospDoctor();
        $doctorUserInfo = $doctorModel->getDataForWhere(array('tel' => $phone));
        if (empty($doctorUserInfo)) {
            $return['err'] = 1;
            $return['msg'] = '系统检查到该手机号用户不是医生';
            return json($return);
        }

        //更具token获取用户
        $tokenUserInfo = $this->getHospWechatRelation(["wechat_access_token" => $access_token, 'ty' => 2, 'hosp_id' => $_hosp]);


        //查询短信验证码
        $tmsgcodeModel = new \app\admin\model\Tmsgcode();
        $tmsgcodeData = $tmsgcodeModel->getDataForWhere(array(
            'tel' => $phone,
            'types' => 10,//注册
        ));

        if (empty($tmsgcodeData)) {
            $return['err'] = 10000;
            $return['msg'] = '短信验证码不存在';
            return json($return);
        }
        if ($tmsgcodeData['codes'] != $rcode) {
            $return['err'] = 10000;
            $return['msg'] = '短信验证码不正确';
            return json($return);
        }
        if ($tmsgcodeData['uses'] > 0) {
            $return['err'] = 10000;
            $return['msg'] = '短信验证码已使用';
            return json($return);
        }


        $inputData = array(
            'head' => $avatar_url,
            'nickname' => $nickname,
            'sex' => $sex,
        );
        $res = $dataModel->updateDataForWhere($inputData, array('id' => $phoneUserInfo['id']));
        if ($res || $res === 0) {
            $phoneUserInfoRelatin = $this->getHospWechatRelation(["uid" => $phoneUserInfo['id'], 'ty' => 2, 'hosp_id' => $_hosp]);
            if (empty($phoneUserInfoRelatin)) {
                if (!empty($tokenUserInfo)) {
                    (new \app\admin\model\HospWechatRelation())->delData(['id' => $tokenUserInfo['id']]);
                }
                (new \app\admin\model\HospWechatRelation())->add([
                    'ty' => 2,
                    'hosp_id' => $_hosp,
                    'uid' => $phoneUserInfo['id'],
                    'wechat_access_token' => $access_token,
                    'wechat_session_key' => $wechat_session_key
                ]);
            } else {
                if (!empty($tokenUserInfo)) {
                    if ($tokenUserInfo['id'] != $phoneUserInfoRelatin['id']) {
                        (new \app\admin\model\HospWechatRelation())->delData(['id' => $tokenUserInfo['id']]);
                    }
                }
                (new \app\admin\model\HospWechatRelation())->updateDataForWhere([
                    'ty' => 2,
                    'uid' => $phoneUserInfo['id'],
                    'wechat_access_token' => $access_token,
                    'wechat_session_key' => $wechat_session_key
                ], [
                    'id' => $phoneUserInfoRelatin['id']
                ]);
            }

            $tmsgcodeModel->updateDataForWhere(array(
                'mtime' => time(),
                'uses' => 1
            ), array('id' => $tmsgcodeData['id']));

            $returnUserInfo = $dataModel->getDataForWhere(['id' => $phoneUserInfo['id']]);
            unset($returnUserInfo['pwd']);
            $returnUserInfo['doctorInfo'] = changeToDoctor($doctorUserInfo);


            $authConfig = array(
                'id' => $returnUserInfo['id'],
            );
            $returnUserInfo['auth'] = userAuth($authConfig);
            $returnUserInfo['hosptialInfo'] = $hosptialInfo;



            $return['err'] = 0;
            $return['msg'] = '成功';
            $return['data'] = $returnUserInfo;
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '失败';
            return json($return);
        }
    }


    /**
     * 获取一些配置信息
     */
    public function getConfig()
    {
        $config = getHospInfoConf();
        $return['err'] = 0;
        $return['msg'] = '成功';
        $return['data'] = $config;
        return json($return);
    }
    public function bindUserBAK1()
    {
        $access_token = trim(input("request.access_token"));//小程序token
        $wechat_session_key = trim(input("request.session_key"));//小程序token
        $avatar_url = trim(input("request.avatar_url"));//头像
        $nickname = trim(input("request.nickname"));//
        $sex = trim(input("request.sex"));//
//        $phone = trim(input("request.phone"));//
        $_hosp = trim($_REQUEST['_hosp']);
//        $rcode = trim(input("request.rcode"));//
        if (empty($_hosp)) {
            $return['err'] = 1;
            $return['msg'] = '参数错误[_hosp]';
            return json($return);
        }

        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }
        if (empty($access_token)) {
            $return['err'] = 10000;
            $return['msg'] = 'access_token错误';
            return json($return);
        }

        if (empty($wechat_session_key)) {
            $return['err'] = 10000;
            $return['msg'] = 'session_key错误';
            return json($return);
        }

        $inputData = array(
            'head' => $avatar_url,
            'nickname' => $nickname,
            'sex' => $sex,
        );

        $dataModel = new \app\admin\model\User();
        //更具token获取用户
        $tokenUserInfo = $this->getHospWechatRelation(["wechat_access_token" => $access_token, 'ty' => 1, 'hosp_id' => $_hosp]);

        if(!empty($tokenUserInfo)) {
            $phoneUserInfo = $dataModel->getDataForWhere(["id" => $tokenUserInfo['uid']]);
            if ($phoneUserInfo) {
                if ($phoneUserInfo['is_delete'] == 1) {
                    (new \app\admin\model\User())->updateDataForWhere([
                        'is_delete' => 0,
                    ], [
                        'id' => $phoneUserInfo['id']
                    ]);
                }
            }else{
                $inputData['uname'] = time();
                $inputData['ctime'] = date("Y-m-d H:i:s");
                $tempUid = $dataModel->add($inputData);
                $phoneUserInfo = $dataModel->getDataForWhere(["id" => $tempUid]);
            }
            addUserSystemGroup($phoneUserInfo['id'], 4);
        }
        else{
                $inputData['uname'] = time();
                $inputData['ctime'] = date("Y-m-d H:i:s");
                $tempUid = $dataModel->add($inputData);
                $phoneUserInfo = $dataModel->getDataForWhere(["id" => $tempUid]);
                (new \app\admin\model\HospWechatRelation())->add([
                    'ty' => 1,
                    'hosp_id' => $_hosp,
                    'uid' => $phoneUserInfo['id'],
                    'wechat_access_token' => $access_token,
                    'wechat_session_key' => $wechat_session_key
                ]);
            addUserSystemGroup($phoneUserInfo['id'], 4);
        }

        unset($phoneUserInfo['pwd']);
        $return['err'] = 0;
        $return['msg'] = '成功';
        $authConfig = array(
            'id' => $phoneUserInfo['id'],
        );
        $returnUserInfo['auth'] = userAuth($authConfig);
        $returnUserInfo['hosptialInfo'] = $hosptialInfo;
        $return['data'] = $returnUserInfo;
        return json($return);
    }

    //小程序绑定用户，询诊端 需要手机号

    public function bindUser()
    {
        $access_token = trim(input("request.access_token"));//小程序token
        $wechat_session_key = trim(input("request.session_key"));//小程序token
        $avatar_url = trim(input("request.avatar_url"));//头像
        $nickname = trim(input("request.nickname"));//
        $sex = trim(input("request.sex"));//
        $phone = trim(input("request.phone"));//
        $_hosp = trim($_REQUEST['_hosp']);
        $rcode = trim(input("request.rcode"));//
        $from_source = trim(input("request.from_source"));//
        if (empty($_hosp)) {
            $return['err'] = 1;
            $return['msg'] = '参数错误[_hosp]';
            return json($return);
        }

        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }
        if (empty($access_token)) {
            $return['err'] = 10000;
            $return['msg'] = 'access_token错误';
            return json($return);
        }

        if (empty($wechat_session_key)) {
            $return['err'] = 10000;
            $return['msg'] = 'session_key错误';
            return json($return);
        }

        if (empty($phone)) {
            $return['err'] = 1;
            $return['msg'] = '手机号不能为空';
            return json($return);
        }
//        if (empty($rcode)) {
//            $return['err'] = 1;
//            $return['msg'] = '验证码不能为空';
//            return json($return);
//        }

        $inputData = array(
            'head' => $avatar_url,
            'nickname' => $nickname,
            'sex' => $sex,
        );


        //查询短信验证码
//        $tmsgcodeModel = new \app\admin\model\Tmsgcode();
//        $tmsgcodeData = $tmsgcodeModel->getDataForWhere(array(
//            'tel' => $phone,
//            'types' => 10,//注册
//        ));
//
//        if (empty($tmsgcodeData)) {
//            $return['err'] = 10000;
//            $return['msg'] = '短信验证码不存在';
//            return json($return);
//        }
//        if ($tmsgcodeData['codes'] != $rcode) {
//            $return['err'] = 10000;
//            $return['msg'] = '短信验证码不正确';
//            return json($return);
//        }
//        if ($tmsgcodeData['uses'] > 0) {
//            $return['err'] = 10000;
//            $return['msg'] = '短信验证码已使用';
//            return json($return);
//        }


        $dataModel = new \app\admin\model\User();
        //更具token获取用户
        $tokenUserInfo = $this->getHospWechatRelation(["wechat_access_token" => $access_token, 'ty' => 1, 'hosp_id' => $_hosp]);

        //更具手机号获取用户
        $phoneUserInfo = $dataModel->getDataForWhere(["uname" => $phone]);

        if (empty($phoneUserInfo)) {
            $inputData['uname'] = $phone;
            $inputData['ctime'] = date("Y-m-d H:i:s");
            $inputData['from_source'] = $from_source;
            $res = $dataModel->add($inputData);
            if (empty($res)) {
                $return['err'] = 1;
                $return['msg'] = '操作失败';
                return json($return);
            }

        } else {
            $inputData['is_delete']=0;
            $dataModel->updateDataForWhere($inputData, ['id' => $phoneUserInfo['id']]);
        }

        $phoneUserInfo = $dataModel->getDataForWhere(["uname" => $phone, 'is_delete' => 0]);
        addUserSystemGroup($phoneUserInfo['id'], 4);
        $phoneUserInfoRelatin = $this->getHospWechatRelation(["uid" => $phoneUserInfo['id'], 'ty' => 1, 'hosp_id' => $_hosp]);
        if (empty($phoneUserInfoRelatin)) {
            if (!empty($tokenUserInfo)) {
                (new \app\admin\model\HospWechatRelation())->delData(['id' => $tokenUserInfo['id']]);
            }
            (new \app\admin\model\HospWechatRelation())->add([
                'ty' => 1,
                'hosp_id' => $_hosp,
                'uid' => $phoneUserInfo['id'],
                'wechat_access_token' => $access_token,
                'wechat_session_key' => $wechat_session_key
            ]);
        } else {
            if (!empty($tokenUserInfo)) {
                if ($tokenUserInfo['id'] != $phoneUserInfoRelatin['id']) {
                    (new \app\admin\model\HospWechatRelation())->delData(['id' => $tokenUserInfo['id']]);
                }
            }
            (new \app\admin\model\HospWechatRelation())->updateDataForWhere([
                'ty' => 1,
                'uid' => $phoneUserInfo['id'],
                'wechat_access_token' => $access_token,
                'wechat_session_key' => $wechat_session_key
            ], [
                'id' => $phoneUserInfoRelatin['id']
            ]);
        }

//        $tmsgcodeModel->updateDataForWhere(array(
//            'mtime' => time(),
//            'uses' => 1
//        ), array('id' => $tmsgcodeData['id']));
        unset($phoneUserInfo['pwd']);
        $return['err'] = 0;
        $return['msg'] = '成功';

        $authConfig = array(
            'id' => $phoneUserInfo['id'],
        );
        $returnUserInfo['auth'] = userAuth($authConfig);
        $returnUserInfo['hosptialInfo'] = $hosptialInfo;

        $return['data'] = $returnUserInfo;
        return json($return);
    }

    private function getHospWechatRelation($w)
    {
        $model = new \app\admin\model\HospWechatRelation();
        return $model->getDataForWhere($w);
    }

    private function httpRequest($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //执行
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /**
     * 根据access_token 获取用户信息  询诊端
     */
    public function getUserinfoForAccessToken()
    {
        $access_token = trim($_REQUEST['access_token']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($_hosp)) {
            $return['err'] = 1;
            $return['msg'] = '参数错误[_hosp]';
            return json($return);
        }
        $userInfo = getUserinfoForAccessToken($access_token,1,$_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        unset($userInfo['pwd']);
        if ($userInfo['is_doctor'] == 1) {
            $doctorModel = new \app\admin\model\HospDoctor();
            $doctorUserInfo = $doctorModel->getDataForWhere(array('tel' => $userInfo['uname']));
            $userInfo['doctorInfo'] = changeToDoctor($doctorUserInfo);
        }

        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }

        $authConfig = array(
            'id' => $userInfo['id'],
        );
        $userInfo['auth'] = userAuth($authConfig);
        $userInfo['hosptialInfo'] = $hosptialInfo;

        $return['err'] = 0;
        $return['msg'] = '成功';
        $return['data'] = $userInfo;
        return json($return);
    }



    public function getUserinfoForIdE()
    {
        $uid = trim($_REQUEST['uid']);
        if (empty($uid)) {
            $return['err'] = 10009;
            $return['msg'] = 'uid不正确';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($_hosp)) {
            $return['err'] = 1;
            $return['msg'] = '参数错误[_hosp]';
            return json($return);
        }
        $userInfo = getUserinfoForUid($uid,1,$_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        unset($userInfo['pwd']);
        if ($userInfo['is_doctor'] == 1) {
            $doctorModel = new \app\admin\model\HospDoctor();
            $doctorUserInfo = $doctorModel->getDataForWhere(array('tel' => $userInfo['uname']));
            $userInfo['doctorInfo'] = changeToDoctor($doctorUserInfo);
        }

        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }

        $authConfig = array(
            'id' => $userInfo['id'],
        );
        $userInfo['auth'] = userAuth($authConfig);
        $userInfo['hosptialInfo'] = $hosptialInfo;

        $return['err'] = 0;
        $return['msg'] = '成功';
        $return['data'] = $userInfo;
        return json($return);
    }

    /**
     * 根据access_token 获取用户信息  医生端
     */
    public function getDoctorUserinfoForAccessToken()
    {
        $access_token = trim($_REQUEST['access_token']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();

        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($_hosp)) {
            $return['err'] = 1;
            $return['msg'] = '参数错误[_hosp]';
            return json($return);
        }
        $userInfo = getUserinfoForAccessToken($access_token,2,$_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        unset($userInfo['pwd']);
        if ($userInfo['is_doctor'] == 1) {
            $doctorModel = new \app\admin\model\HospDoctor();
            $doctorUserInfo = $doctorModel->getDataForWhere(array('tel' => $userInfo['uname']));
            $userInfo['doctorInfo'] = changeToDoctor($doctorUserInfo);
        }

        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }

        $authConfig = array(
            'id' => $userInfo['id'],
        );
        $userInfo['auth'] = userAuth($authConfig);
        $userInfo['hosptialInfo'] = $hosptialInfo;

        $return['err'] = 0;
        $return['msg'] = '成功';
        $return['data'] = $userInfo;
        return json($return);
    }


    /**
     * 根据UID 获取用户信息
     */
    public function getUserInfoForId()
    {
        $access_token = trim($_REQUEST['access_token']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($_hosp)) {
            $return['err'] = 1;
            $return['msg'] = '参数错误[_hosp]';
            return json($return);
        }
        $userInfo = getUserinfoForAccessToken($access_token,1,$_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }
        $uid = trim($_REQUEST['uid']);

        $selectUserInfo = getUserInfoForID($uid);
        if (empty($selectUserInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        if (empty($selectUserInfo['is_doctor'])) {
            $return['err'] = 10009;
            $return['msg'] = '该用户还不是医生';
            echo json_encode($return, true);
            exit;
        }

        unset($userInfo['pwd']);
        $doctorModel = new \app\admin\model\HospDoctor();
        $doctorUserInfo = $doctorModel->getDataForWhere(array('uid' => $selectUserInfo['id']));

        $selectUserInfo['doctorInfo'] = changeToDoctor($doctorUserInfo);
        $selectUserInfo['serviceItem'] = getDoctorServiceItem($doctorUserInfo['uid']);

        $return['err'] = 0;
        $return['msg'] = '成功';
        $return['data'] = $selectUserInfo;
        return json($return);
    }


    /**
     * 完善用户信息
     * 补充用户数据
     */
    public function updateUserInfo()
    {
        $access_token = trim($_REQUEST['access_token']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($_hosp)) {
            $return['err'] = 1;
            $return['msg'] = '参数错误[_hosp]';
            return json($return);
        }
        $userInfo = getUserinfoForAccessToken($access_token,1,$_hosp);
        if (empty($userInfo)) {
            $userInfo = getUserinfoForAccessToken($access_token,2,$_hosp);
            if (empty($userInfo)) {
                $return['err'] = 10009;
                $return['msg'] = '未找到相关用户';
                echo json_encode($return, true);
                exit;
            }
        }

        $sex = trim(input("request.sex"));//用户
        $bday = trim(input("request.bday"));//用户auth
        $height = trim(input("request.height"));//用户auth
        $weight = trim(input("request.weight"));//用户auth
        $nickname = trim(input("request.nickname"));//用户auth
        $sleep_option = trim(input("request.sleep_option"));//失眠类型
        $head = trim(input("request.head"));//失眠类型
        $model = new \app\admin\model\User();
        $update = array();
        if ($sex != "") {
            $update['sex'] = $sex;
        }
        if (!empty($bday)) {
            $update['bday'] = $bday;
        }
        if (!empty($height)) {
            $update['height'] = $height;
        }
        if (!empty($weight)) {
            $update['weight'] = $weight;
        }
        if (!empty($nickname)) {
            $update['nickname'] = $nickname;
        }
        if (!empty($sleep_option)) {
            $update['sleep_option'] = $sleep_option;
        }
        if (!empty($head)) {
            $update['head'] = $head;
        }
        if (empty($update)) {
            $return['err'] = 1;
            $return['msg'] = '请设置要修改的内容';
            return json($return);
        }

        $flag = $model->updateData($update, $userInfo['id']);
        if ($flag || $flag == 0) {
            $return['err'] = 0;
            $return['msg'] = '操作成功';
            return json($return);
        } else {
            $return['err'] = 1;
            $return['msg'] = '操作失败';
            return json($return);
        }
    }

    public function js()
    {
        hospOrderJs(2);
    }


    /**
     * 患者端 我的医生列表接口
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function myDoctor()
    {
        $access_token = trim($_REQUEST['access_token']);
        $_hosp = trim($_REQUEST['_hosp']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        if (empty($_hosp)) {
            $return['err'] = 10009;
            $return['msg'] = '参数错误';
            echo json_encode($return, true);
            exit;
        }
        $hosptialInfo = getHospInfo($_hosp);
        if (empty($hosptialInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '医院信息不存在';
            echo json_encode($return, true);
            exit;
        }


        $userModel = new \app\admin\model\User();
        $userInfo = getUserinfoForAccessToken($access_token, 1, $_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }

        $pagesize = trim(input("pagesize"));//每页条数

        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }

        $w = array('is_delete' => 0, 'is_cancel' => 0, 'hosp_id' => $_hosp,'stat'=>1,'status'=>array('in',"2,3,4"),'doctor_id'=>array('gt',0));


        $w['uid'] = $userInfo['id'];
        $orderby = "id desc";
        $model = new \app\admin\model\HospOrder();
        $lists = $model->listsNorepeat($w, $this->pagesize, 'doctor_id', $orderby,"doctor_id");
        foreach ($lists['data'] as $key => $val) {
            $lists['data'][$key]['DoctorInfo'] = changeToDoctor(getDoctorInfo($val['doctor_id']));

        }
        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);
    }


    public function newWindBindUser()
    {
        $access_token = trim(input("request.openid"));//小程序token
        $wechat_session_key = trim(input("request.session_key"));//小程序token
        $avatar_url = trim(input("request.avatar_url"));//头像
        $nickname = trim(input("request.nickname"));//
        $sex = trim(input("request.sex"));//
        $phone = trim(input("request.phone"));//
        $_hosp = 1;
        $rcode = trim(input("request.rcode"));//
        $from_source = trim(input("request.from_source"));//

        if (empty($access_token)) {
            $return['err'] = 10000;
            $return['msg'] = 'access_token错误';
            return json($return);
        }

        if (empty($wechat_session_key)) {
            $return['err'] = 10000;
            $return['msg'] = 'session_key错误';
            return json($return);
        }

        if (empty($phone)) {
            $return['err'] = 1;
            $return['msg'] = '手机号不能为空';
            return json($return);
        }
//        if (empty($rcode)) {
//            $return['err'] = 1;
//            $return['msg'] = '验证码不能为空';
//            return json($return);
//        }

        $inputData = array(
            'head' => $avatar_url,
            'nickname' => $nickname,
            'sex' => $sex,
        );


        //查询短信验证码
//        $tmsgcodeModel = new \app\admin\model\Tmsgcode();
//        $tmsgcodeData = $tmsgcodeModel->getDataForWhere(array(
//            'tel' => $phone,
//            'types' => 10,//注册
//        ));
//
//        if (empty($tmsgcodeData)) {
//            $return['err'] = 10000;
//            $return['msg'] = '短信验证码不存在';
//            return json($return);
//        }
//        if ($tmsgcodeData['codes'] != $rcode) {
//            $return['err'] = 10000;
//            $return['msg'] = '短信验证码不正确';
//            return json($return);
//        }
//        if ($tmsgcodeData['uses'] > 0) {
//            $return['err'] = 10000;
//            $return['msg'] = '短信验证码已使用';
//            return json($return);
//        }


        $dataModel = new \app\admin\model\User();
        //更具token获取用户
        $tokenUserInfo = $this->getHospWechatRelation(["wechat_access_token" => $access_token, 'ty' => 1, 'hosp_id' => $_hosp]);

        //更具手机号获取用户
        $phoneUserInfo = $dataModel->getDataForWhere(["uname" => $phone]);

        if (empty($phoneUserInfo)) {
            $inputData['uname'] = $phone;
            $inputData['ctime'] = date("Y-m-d H:i:s");
            $inputData['from_source'] = $from_source;
            $res = $dataModel->add($inputData);
            if (empty($res)) {
                $return['err'] = 1;
                $return['msg'] = '操作失败';
                return json($return);
            }

            $NewwindRoomModel = new \app\admin\model\NewwindRoom();
            $NewwindRoomModel->add(array(
                'uid' => $res,
                'ctime' => date("Y-m-d H:i:s"),
                'name' => "默认",
                'flags' => 1,
            ));

        } else {
            $inputData['is_delete']=0;
            $dataModel->updateDataForWhere($inputData, ['id' => $phoneUserInfo['id']]);
        }

        $phoneUserInfo = $dataModel->getDataForWhere(["uname" => $phone, 'is_delete' => 0]);
        addUserSystemGroup($phoneUserInfo['id'], 4);
        $phoneUserInfoRelatin = $this->getHospWechatRelation(["uid" => $phoneUserInfo['id'], 'ty' => 1, 'hosp_id' => $_hosp]);
        if (empty($phoneUserInfoRelatin)) {
            if (!empty($tokenUserInfo)) {
                (new \app\admin\model\HospWechatRelation())->delData(['id' => $tokenUserInfo['id']]);
            }
            (new \app\admin\model\HospWechatRelation())->add([
                'ty' => 1,
                'hosp_id' => $_hosp,
                'uid' => $phoneUserInfo['id'],
                'wechat_access_token' => $access_token,
                'wechat_session_key' => $wechat_session_key
            ]);
        } else {
            if (!empty($tokenUserInfo)) {
                if ($tokenUserInfo['id'] != $phoneUserInfoRelatin['id']) {
                    (new \app\admin\model\HospWechatRelation())->delData(['id' => $tokenUserInfo['id']]);
                }
            }
            (new \app\admin\model\HospWechatRelation())->updateDataForWhere([
                'ty' => 1,
                'uid' => $phoneUserInfo['id'],
                'wechat_access_token' => $access_token,
                'wechat_session_key' => $wechat_session_key
            ], [
                'id' => $phoneUserInfoRelatin['id']
            ]);
        }

//        $tmsgcodeModel->updateDataForWhere(array(
//            'mtime' => time(),
//            'uses' => 1
//        ), array('id' => $tmsgcodeData['id']));
        unset($phoneUserInfo['pwd']);
        $return['err'] = 0;
        $return['msg'] = '成功';

        $authConfig = array(
            'id' => $phoneUserInfo['id'],
        );
        $returnUserInfo['auth'] = userAuth($authConfig);
        $defaultRoom = (new \app\admin\model\NewwindRoom())->getDataForWhere(['uid'=>$phoneUserInfo['id'],'flags'=>1]);
        $returnUserInfo['default_room_id']=$defaultRoom['id'];
        $return['data'] = $returnUserInfo;
        return json($return);
    }


    public function getNewWindWechatOpenid()
    {
        $_hosp = 1;
        $code = trim(input("code"));//用户auth
        $Config = config('WEIXIN_APP');
        $Config = $Config[$_hosp];
        $Config = $Config['userClient'];

        $appid = $Config['APPID'];
        $secret = $Config['APPSECRET'];
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";
        $res = $this->httpRequest($url);
        $res = json_decode($res, true);
        if ($res['session_key'] && $res['openid']) {
            $return['err'] = 0;
            $return['data'] = $res;
            $return['msg'] = '成功';
            return json($return);
        } else {
            $return['err'] = 1;
            $return['data'] = $res;
            $return['msg'] = '失败';
            return json($return);
        }
    }





    public function getNewwindUserinfoForOpenid()
    {
        $access_token = trim($_REQUEST['openid']);
        if (empty($access_token)) {
            $return['err'] = 10009;
            $return['msg'] = 'access_token不正确';
            echo json_encode($return, true);
            exit;
        }
        $userModel = new \app\admin\model\User();

        $_hosp = 1;

        $userInfo = getUserinfoForAccessToken($access_token,1,$_hosp);
        if (empty($userInfo)) {
            $return['err'] = 10009;
            $return['msg'] = '未找到相关用户';
            echo json_encode($return, true);
            exit;
        }
        unset($userInfo['pwd']);
        $authConfig = array(
            'id' => $userInfo['id'],
        );

        //获取默认房价ID
        $defaultRoom = (new \app\admin\model\NewwindRoom())->getDataForWhere(['uid'=>$userInfo['id'],'flags'=>1]);
        $userInfo['default_room_id']=$defaultRoom['id'];
        $userInfo['auth'] = userAuth($authConfig);
        $return['err'] = 0;
        $return['msg'] = '成功';
        $return['data'] = $userInfo;
        return json($return);
    }



    public function getNewwindUserinfoForAuth()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }

        unset($userInfo['pwd']);
        $authConfig = array(
            'id' => $userInfo['id'],
        );

        //获取默认房价ID
        $defaultRoom = (new \app\admin\model\NewwindRoom())->getDataForWhere(['uid'=>$userInfo['id'],'flags'=>1]);
        $userInfo['default_room_id']=$defaultRoom['id'];

        $userInfo['auth'] = userAuth($authConfig);
        $return['err'] = 0;
        $return['msg'] = '成功';
        $return['data'] = $userInfo;
        return json($return);
    }



}

